package cn.edu.ljl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

//在登录成功的前提下，对成绩信息进行查询

public class refer extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String name = request.getParameter("nam");
        String key  =name;
        //String sql =" select *  from grade where nam = '"+name+"'";
        String sql="SELECT * FROM `grade` WHERE" +
                " `nam` LIKE '%" + key + "%'" +
                " OR `subje` LIKE '%" + key + "%'" ;
        System.out.println(sql);

        try {
            List<Student> students=DBMS.getStudents(sql);
            response.setCharacterEncoding("UTF-8");
            Writer writer = response.getWriter();
            StringBuilder sb =new StringBuilder();

            sb.append(Page.getHeader());
            sb.append(toHtml(students));
            sb.append(Page.getEnd());
            writer.write(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private String toHtml (List<Student> students){
        StringBuilder sb =new StringBuilder();
        sb.append("<center><br><table style='width:65%' border='0' cellpadding='10'>");

        sb.append("<tr style='background-color:#747474;color:#ff5400'>" +
                "<th>编号</th><th>学号</th><th>名字</th><th>科目</th><th>分数</th><th>感想</th>" +
                "<th></th><th></th></tr>");
        for (int i=0; i<students.size(); i++) {
            Student student =students.get(i);
            if (i % 6 == 0) {
                sb.append("<tr style='background-color:#f0e68c'>");
            } else if(i % 6==1){
                sb.append("<tr style='background-color:#ffff99'>");
            }else if(i % 6==2){
                sb.append("<tr style='background-color:#30d5c8'>");
            }else if(i % 6==3){
                sb.append("<tr style='background-color:#0dbf8c'>");
            }else if(i % 6==4){
                sb.append("<tr style='background-color:#faf0e6'>");
            }else if(i % 6==5){
                sb.append("<tr style='background-color:#eac096'>");
            }
            sb.append("<td>").append(student.getID()).append("</td>");
            sb.append("<td><b>").append(student.getNumber()).append("</b></td>");
            sb.append("<td>").append(student.getName()).append("</td>");
            sb.append("<td>").append(student.getSubject()).append("</td>");
            sb.append("<td>").append(student.getScore()).append("</td>");
            sb.append("<td>").append(student.getFeel()).append("</td>");
            sb.append("<td>").append(
                    String.format("<a href='./update?id=%s'>修改</a>", student.getID())
            ).append("</td>");
            sb.append("<td>").append(
                    String.format("<a href='./delete?id=%s'>删除</a>", student.getID())
            ).append("</td>");
            sb.append("</tr>");
        }
        sb.append("</table></center>");
        return sb.toString();

    }
}


// select *  from grade where nam = "郑瑞旭" ;